﻿using System;
using System.Web.Mvc;
using XRM.Security.WebUI.Controllers;
using XRM.Security.WebUI.Models;

namespace XRM.Security.WebUI.App_Start
{
    public class CustomModelBinderProvider : IModelBinderProvider
    {
        public IModelBinder GetBinder(Type modelType)
        {
            return new CustomModelBinder();
        }
    }

    public class CustomModelBinder : DefaultModelBinder
    {
        protected override void OnModelUpdated( ControllerContext controllerContext, ModelBindingContext bindingContext )
        {
            var model = bindingContext.Model as BaseViewModel;
            if ( model != null )
            {
                model.SetController( controllerContext.Controller as BaseController );
            }
            base.OnModelUpdated( controllerContext, bindingContext );
        }
    }
}